package leecode

type NumMatrixLCR struct {
	prefix [][]int //定义一个每一行的前缀和数组
}

func ConstructorLCR13(matrix [][]int) NumMatrixLCR {
	prefix := make([][]int, len(matrix)) //行数相同
	for i := 0; i < len(matrix); i++ {
		prefix[i] = make([]int, len(matrix[i])+1)
	}
	//求每一行元素的前缀和
	for i := 0; i < len(matrix); i++ {
		for j := 0; j < len(matrix[i]); j++ {
			prefix[i][j+1] = prefix[i][j] + matrix[i][j]
		}
	}
	return NumMatrixLCR{prefix}
}

func (this *NumMatrixLCR) SumRegion(row1 int, col1 int, row2 int, col2 int) int {
	var result int
	for row1 <= row2 {
		result += this.prefix[row1][col2+1] - this.prefix[row1][col1]
		row1++
	}
	return result
}
